package com.base;


import java.io.Serializable;
import java.util.List;



import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;


/**
 * 数据库操作基础类
 * 此类实现了对数据库的操作
 * 并提供SessionFactory对象，子类可通过SessionFactory拓展对数据库的操作
 * @author 
 *
 */
public class BaseDao{

    protected SessionFactory sessionfactory; 
	
    protected HibernateTemplate hibernateTemplate;
    
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	
	public void setSessionfactory(SessionFactory sessionfactory) {
		this.sessionfactory = sessionfactory;
	}
	
	public Object save(BaseEntity o) throws Exception {
		try {
			Object b=getHibernateTemplate().save(o);
			return b;
		} catch (RuntimeException re) {
			re.printStackTrace();
			throw re;
		}
	}
	
	public void saveOrUpdate(BaseEntity o) throws Exception {
		try {
			getHibernateTemplate().saveOrUpdate(o);
		} catch (RuntimeException re) {
			re.printStackTrace();
			throw re;
		}
	}
	
	
	public void delete(BaseEntity o) throws Exception {
		try {
			getHibernateTemplate().delete(o);
		} catch (RuntimeException re) {
			re.printStackTrace();
			throw re;
		}
	}
	
	public void deleteAll(List<BaseEntity> list) throws Exception {
		try {
			getHibernateTemplate().deleteAll(list);
		} catch (RuntimeException re) {
			re.printStackTrace();
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public Object findById(Class classObj, Serializable id) throws Exception {
		try {
			Object instance = (Object) getHibernateTemplate().get(classObj, id);
			return instance;
		} catch (RuntimeException re) {
			re.printStackTrace();
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List find(String strHQL) throws Exception {
		try {
			return getHibernateTemplate().find(strHQL);
		} catch (RuntimeException re) {
			re.printStackTrace();
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List find(final String strHQL, final List param, final int first, final int max) throws Exception {
		try {
			return getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session s) throws HibernateException {
					Query query = s.createQuery(strHQL);
					if (param != null && param.size() > 0) {
						for (int j = 0; j < param.size(); j++) {
							query.setParameter(j, param.get(j));
						}
					}
					query.setFirstResult(first);
					query.setMaxResults(max);
					List list = query.list();
					return list;
				}
			});
		} catch (RuntimeException re) {
			re.printStackTrace();
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List find(final String strHQL, final List param) throws Exception {
		try {
			return getHibernateTemplate().executeFind(new HibernateCallback() {
				public Object doInHibernate(Session s) throws HibernateException {
					Query query = s.createQuery(strHQL);
					if (param != null && param.size() > 0) {
						for (int j = 0; j < param.size(); j++) {
							query.setParameter(j, param.get(j));
						}
					}
					List list = query.list();
					return list;
				}
			});
		} catch (RuntimeException re) {
			re.printStackTrace();
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List findByExample(BaseEntity o) throws Exception {
		try {
			List results = getHibernateTemplate().findByExample(o);
			return results;
		} catch (RuntimeException re) {
			re.printStackTrace();
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List findByProperty(String propertyName, Object value) throws Exception {
		try {
			String queryString = "from TBaseFile as model where model." + propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			re.printStackTrace();
			throw re;
		}
	}

	
}